// AudioPlayer.java
package game;

import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;

public class AudioPlayer {

    private static Clip musicClip;
    private static Clip effectClip;
    private static boolean isMusicMuted = false; // 跟踪背景音乐是否被静音的标志
    private static boolean isEffectMuted = false; // 跟踪音效是否被静音的标志

    private static final String SKILL_LEARN_SOUND_PATH = "C:\\Users\\GeekGuru\\Downloads\\魔法钢琴旋律循环.wav";

    public static void playSound(String filePath, boolean loop, boolean isBackgroundMusic, float volume) {
        if ((isBackgroundMusic && isMusicMuted) || (!isBackgroundMusic && isEffectMuted)) {
            return; // 如果被静音，则不播放声音
        }

        try {
            File audioFile = new File(filePath);
            if (!audioFile.exists()) {
                System.err.println("音频文件未找到：" + filePath);
                return;
            }

            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(audioFile);
            AudioFormat baseFormat = audioInputStream.getFormat();
            AudioFormat decodedFormat = new AudioFormat(
                    AudioFormat.Encoding.PCM_SIGNED,
                    baseFormat.getSampleRate(),
                    16,
                    baseFormat.getChannels(),
                    baseFormat.getChannels() * 2,
                    baseFormat.getSampleRate(),
                    false
            );

            AudioInputStream decodedAudioInputStream = AudioSystem.getAudioInputStream(decodedFormat, audioInputStream);

            Clip clip;
            if (isBackgroundMusic) {
                if (musicClip != null && musicClip.isOpen()) {
                    musicClip.stop();
                    musicClip.close();
                }
                musicClip = AudioSystem.getClip();
                clip = musicClip;
            } else {
                if (effectClip != null && effectClip.isOpen()) {
                    effectClip.stop();
                    effectClip.close();
                }
                effectClip = AudioSystem.getClip();
                clip = effectClip;
            }

            clip.open(decodedAudioInputStream);
            if (clip.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
                FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
                gainControl.setValue(volume); // 调整音量
            }

            clip.addLineListener(event -> {
                if (event.getType() == LineEvent.Type.STOP) {
                    event.getLine().close();
                }
            });

            if (loop) {
                clip.loop(Clip.LOOP_CONTINUOUSLY);
            } else {
                clip.start();
            }
        } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
            System.err.println("播放声音失败：" + e.getMessage());
        }
    }

    public static void stopMusic() {
        if (musicClip != null) {
            musicClip.stop();
            musicClip.close();
        }
    }

    public static void stopEffect() {
        if (effectClip != null) {
            effectClip.stop();
            effectClip.close();
        }
    }

    public static void setVolume(float volume, boolean isBackgroundMusic) {
        Clip targetClip = isBackgroundMusic ? musicClip : effectClip;
        if (targetClip != null && targetClip.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
            FloatControl gainControl = (FloatControl) targetClip.getControl(FloatControl.Type.MASTER_GAIN);
            gainControl.setValue(volume); // 设置音量
        }
    }

    public static void playSkillLearnSound() {
        stopMusic(); // 停止背景音乐
        playSound(SKILL_LEARN_SOUND_PATH, true, true, -10.0f); // 播放学习技能的声音
    }

    public static void toggleMusicMute() {
        isMusicMuted = !isMusicMuted;
        if (isMusicMuted) {
            stopMusic();
        }
    }

    public static void toggleEffectMute() {
        isEffectMuted = !isEffectMuted;
        if (isEffectMuted) {
            stopEffect();
        }
    }
}